package org.yaac.shared.property;

import static com.google.common.collect.Lists.newArrayList;

import java.util.Arrays;
import java.util.List;

import org.yaac.shared.egql.ResultCell;
import org.yaac.shared.egql.ResultCell.ResultCellFactory;




/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
//TODO : implement ordering based on logics documented at http://code.google.com/appengine/docs/java/datastore/entities.html
public class GeoPtPropertyInfo extends PropertyInfo {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Float latitude;

	private Float longitude;
	
	@SuppressWarnings("unused")
	private GeoPtPropertyInfo(){}
	
	public GeoPtPropertyInfo(Float latitude, Float longitude) {
		super();
		this.latitude = latitude;
		this.longitude = longitude;
	}
	
	public Float getLatitude() {
		return latitude;
	}

	public Float getLongitude() {
		return longitude;
	}

	@Override
	public int compareTo(PropertyInfo o) {
		if (o instanceof GeoPtPropertyInfo) {
			int cp1 = this.latitude.compareTo(((GeoPtPropertyInfo) o).latitude);
			if (cp1 == 0) {
				return this.longitude.compareTo(((GeoPtPropertyInfo) o).longitude);
			} else {
				return cp1;
			}
		} else {
			return 0;	
		}
	}

	@Override
	public String asHtml() {
		// TODO locale?
		return latitude + ":" + longitude;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((latitude == null) ? 0 : latitude.hashCode());
		result = prime * result + ((longitude == null) ? 0 : longitude.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GeoPtPropertyInfo other = (GeoPtPropertyInfo) obj;
		if (latitude == null) {
			if (other.latitude != null)
				return false;
		} else if (!latitude.equals(other.latitude))
			return false;
		if (longitude == null) {
			if (other.longitude != null)
				return false;
		} else if (!longitude.equals(other.longitude))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "GeoPtPropertyInfo [latitude=" + latitude + ", longitude=" + longitude + "]";
	}

	@Override
	public ResultCell populateResultCell(ResultCellFactory factory) {
		ResultCell cell = super.populateResultCell(factory);
		cell.setType(PropertyType.GEOPT);
		cell.setParent(null);
		cell.setChildren(null);
		cell.setPayload(Arrays.asList(latitude.toString(), longitude.toString()));
		return cell;
	}

	@Override
	public String asExpression(FilePlaceHolder placeHolder) {
		return "geopt(" + latitude + "," + longitude + ")";
	}
	
	@Override
	public List<String> downloadPaths() {
		return newArrayList();
	}
}
